package cli;

import database.DatabaseHelper;
import entity.Student;
import javafx.scene.control.Tab;
import statistic.CourseScoreList;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Array;
import java.sql.SQLException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class StudentScore implements CommandLineInterface.Handler {

    DatabaseHelper db;

    private Student inputStudent(Scanner in, PrintWriter out) throws SQLException {
        System.out.println("请输入学生学号或姓名:");
        String str = in.next();
        String sid;
        if (str.matches("[a-zA-Z0-9]*[0-9][a-zA-Z0-9]*")) {
            sid = str;
        } else {
            ArrayList<Student> students = Student.findByName(db, str);
            if (students.isEmpty()) {
                return null;
            }
            if (students.size() == 1) {
                sid = students.get(0).getId();
            } else {
                out.println("有多个学生重名：");
                String[] header = {"学号", "姓名", "性别"};
                String[][] table = new String[students.size()][header.length];
                for (int i = 0; i < students.size(); ++i) {
                    table[i][0] = students.get(i).getId();
                    table[i][1] = students.get(i).getName();
                    table[i][2] = students.get(i).getSex();
                }
                out.print(new TableLayout(header, table));
                out.println("请输入学生学号：");
                sid = in.next();
            }
        }
        return Student.find(db, sid);
    }

    private void output(PrintWriter out, CourseScoreList list) {
        if (list == null)
            return;
        String[] header = {"学期", "课程", "平时成绩", "期中成绩", "实验成绩", "期末成绩", "总成绩", "取得时间"};
        String[][] table = new String[list.scores.size()][header.length];
        for (int i = 0; i < list.scores.size(); ++i) {
            table[i][0] = list.sections.get(i).getSemester();
            table[i][1] = list.courses.get(i).getName();
            table[i][2] = String.format("%.0f", list.scores.get(i).getUsual());
            table[i][3] = String.format("%.0f", list.scores.get(i).getMidterm());
            table[i][4] = String.format("%.0f", list.scores.get(i).getExperiment());
            table[i][5] = String.format("%.0f", list.scores.get(i).getTerminal());
            table[i][6] = String.format("%.0f", list.scores.get(i).getTotal());
            table[i][7] = list.scores.get(i).getTimeStamp().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
        }
        out.println(new TableLayout(header, table));
        out.println("平均成绩：\t" + String.format("%.1f", list.total / list.scores.size()));
    }

    @Override
    public void handle(Scanner in, PrintWriter out) {
        try {
            db = new DatabaseHelper();
            Student stu = inputStudent(in, out);
            if (stu == null) {
                out.println("学生不存在！");
                return;
            }
            CourseScoreList list = new CourseScoreList(stu);
            output(out, list);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }
}
